﻿// JScript 文件
//全选/全消
function selectAll() {
    var val = document.getElementById("chkSelectAll");
    var rowob = document.getElementsByTagName("input"); //.getElementsByName("contractRow");
    var rowcount = rowob.length;

    for (var i = 1; i < rowcount; i++) {
        rowob[i].checked = val.checked;
    }
}
function selectAllDeliveryApply() {
    var val = document.getElementById("chkSelectAll");
    var rowob = document.getElementsByTagName("input"); //.getElementsByName("contractRow");
    var rowcount = rowob.length;

    for (var i = 1; i < rowcount; i++) {
        rowob[i].checked = val.checked;
    }
    if (val.checked) {
        var all = 0;
        for (var i = 1; i < rowcount; i++) {
            if (rowob[i].checked && rowob[i].id.indexOf("chk") != -1 && rowob[i].name != "contractRow") {
                namestr = rowob[i].name;
                all = all + document.getElementById("txt" + namestr).value * document.getElementById("TradingUnitPrice" + namestr).value * 1.17;
            } 
        }
        document.getElementById("txtApprovalSelectedAmount").value = all;
    }
    else {
        document.getElementById("txtApprovalSelectedAmount").value = "0";
    }
}
function GetSelectedAmount() {
    var rowob = document.getElementsByTagName("input");
    var rowcount = rowob.length;
    var all = 0;
    for (var i = 1; i < rowcount; i++) {
        if (rowob[i].checked && rowob[i].id.indexOf("chk") != -1 && rowob[i].name != "contractRow") {
            namestr = rowob[i].name;
            all = all + document.getElementById("txt" + namestr).value * document.getElementById("TradingUnitPrice" + namestr).value;
        }
    }
    document.getElementById("txtApprovalSelectedAmount").value = Math.round(all * 100000) / 100000;
    //alert(all);
}

//文本框输入只能为非负数
function txtIsNumber(txtID,str) {
    var a = document.getElementById(txtID).value;
    var flag = true;
    if (!(Math.abs(a) == a) || a == "") {
        alert("只能是非负数,也不能为空-Not less than zero！");
        document.getElementById(txtID).value = "0";
        document.getElementById(txtID).focus();
        return false;
    }
    a = document.getElementById(txtID).value;
    if (flag && a > 0) {
        if (str > 0 && str < a) {
            document.getElementById(txtID).value = str;
            alert("bigger！");
            return false
        }
        return true;
    }
}
function showHiddentxtddl(n, txtID, dllID) {
    if (n == 1) {
        document.getElementById(txtID).style.display = "";
        document.getElementById(dllID).style.display = "none";
    }
    else {
        document.getElementById(txtID).style.display = "none";
        document.getElementById(dllID).style.display = "";
    }
}

function showHidden(strID, strID1, strID2, strID3) {
    document.getElementById(strID).style.display = "none";
    document.getElementById(strID1).style.display = "none";
    document.getElementById(strID2).style.display = "";
    document.getElementById(strID3).style.display = "";
}

function deliveryOnload() {
    document.getElementById("ddlSaleName").style.display = "none";
    document.getElementById("txtSaleName").style.display = "";
    document.getElementById("imgSaleName").style.display = "none";
    document.getElementById("ddlCustomer").style.display = "none";
    document.getElementById("txtCustomer").style.display = "";
    document.getElementById("imgCustomer").style.display = "none";
    document.getElementById("ddlSoNo").style.display = "none";
    document.getElementById("txtSoNo").style.display = "";
    document.getElementById("imgSoNo").style.display = "none";
    document.getElementById("ddlContractNo").style.display = "none";
    document.getElementById("txtContractNo").style.display = "";
    document.getElementById("imgContractNo").style.display = "none";
    document.getElementById("ddlContractNoCustomer").style.display = "none";
    document.getElementById("txtContractNoCustomer").style.display = "";
    document.getElementById("imgContractNoCustomer").style.display = "none";
}
function deliveryOnloadApply() {
    document.getElementById("ddlSaleName").style.display = "none";
    document.getElementById("txtSaleName").style.display = "";
    document.getElementById("imgSaleName").style.display = "";
    document.getElementById("ddlCustomer").style.display = "none";
    document.getElementById("txtCustomer").style.display = "";
    document.getElementById("imgCustomer").style.display = "";
    document.getElementById("ddlSoNo").style.display = "none";
    document.getElementById("txtSoNo").style.display = "";
    document.getElementById("imgSoNo").style.display = "";
    document.getElementById("ddlContractNo").style.display = "none";
    document.getElementById("txtContractNo").style.display = "";
    document.getElementById("imgContractNo").style.display = "";
    document.getElementById("ddlContractNoCustomer").style.display = "none";
    document.getElementById("txtContractNoCustomer").style.display = "";
    document.getElementById("imgContractNoCustomer").style.display = "";
}

function showOrHidden(strID, str) {
    document.getElementById(strID).style.display = str;
}


function ApprovalRemarkUpdateDAO(n, tableID, strID, userCode, coCode, flag) {
    if (n == 1) {
        document.getElementById("txtForRemark").value = document.getElementById(strID).value;
    }
    else {
        var str = document.getElementById(strID).value;
        var temp = document.getElementById("txtForRemark").value;
        if (str != temp) {
            var aa = DeliveryApplyWebService;
            aa.ApprovalRemarkUpdate(tableID, str, userCode, coCode, flag, remrkF);
        }
    }
}
function remrkF(result) {
    if (result > 0) {
//            document.getElementById("txtForRemark").value = "@";
//            window.parent.mainFrame.location.href = window.parent.mainFrame.location.href;
//            window.parent.mainFrame.location.reload(); 
    }
}

function setWidth() {
    $get("detailDiv").style.width = document.body.offsetWidth * 0.99;
}

function showddldiv(strID, n) {
    var a = document.getElementById(strID).value;
    if (n == 0) {
        get(a);
    }
    else if (a.length > 2) {
        get(a);
    }
}
function get(str) {
    var aa = DeliveryApplyWebService;
    aa.GetDivDll(str, getResult);
}
function getResult(result) {
    $("#divContractNo").append(result[0].toString());
}
function saveFile() {
    var rr = document.getElementById("DeliveryApplyFile").value;
    var aa = DeliveryApplyWebService;
    aa.saveFile(str, getResult);
}

function TradingDeliveryApply() {
    document.getElementById("covertDiv").style.display = "block";
    var val = document.getElementById("chkSelectAll");
    var rowob = document.getElementsByTagName("input");
    var rowcount = rowob.length;
    var str = "";
    var namestr = "";

    for (var i = 1; i < rowcount; i++) {
        if (rowob[i].checked && rowob[i].id.indexOf("chk") != -1 && rowob[i].name != "contractRow") {
            namestr = rowob[i].name;
            str = str + rowob[i].name + "/";
            str = str + (Math.round(document.getElementById("txt" + namestr).value * 1000) / 1000).toString() + ",";
        }
    }
//    alert(str);
    if (str.length > 1) {
        var aa = DeliveryApplyWebService;
        var userCode = document.getElementById("txtUserCode").value;
        var coCode = document.getElementById("txtCoCode").value;
        var remark = document.getElementById("txtRemark").value;
        var fileName = DeliveryApplyGetFileName();
        aa.TradingDeliveryApply(userCode, coCode, str, remark, fileName, abda);
    }
}
function abda(str) {
    if (str > 0) {
        window.parent.mainFrame.location.href = window.parent.mainFrame.location.href;
        window.parent.mainFrame.location.reload();
    }
    document.getElementById("covertDiv").style.display = "none";
}
function getResult(result) {
    $("#divContractNo").append(result[0].toString());
}
function DeliveryApplyGetFileName() {
            var obj = document.getElementById("DeliveryApplyFileUploadWeb").contentWindow;
            var obji1 = obj.document.getElementById("txtDeliveryApplyFileName");
            var val = eval(obji1).value;
            return val;
        }

function TradingQTYForShippment(strID, rowID, creitValue) {
    if (txtIsNumber(strID)) {
        var a = document.getElementById(strID).value;
        if (Math.abs(a) > Math.abs(creitValue)) {
            window.alert("必须小于可申请数量!");
            document.getElementById(strID).value = creitValue;
        }
        //                        else if (a < 0) {
        //                            window.alert("It must be bigger than 0!");
        //                            document.getElementById(strID).value = creitValue;
        //                        }
    }
}
function kk(result) {
    if (result > 0) {
        window.alert("success！");
     }
}

function TradingMaterielApportion() {
    document.getElementById("covertDiv").style.display = "block";
    var contractNo = document.getElementById("txtContractNo").value;
    var soNo = document.getElementById("txtSoNo").value;
    var aa = DeliveryApplyWebService;
    var userCode = document.getElementById("txtUserCode").value;
    var coCode = document.getElementById("txtCoCode").value;
    aa.GetDeliverySelectAmount(contractNo, soNo, TradingMaterielApportionResult);            
}
function TradingMaterielApportionResult(result) {
    var ApplyAmount = result;
    if (result < 0.001) {
        ApplyAmount = 1;
    }
    var SelectedAmount = GetSelectedMaterielApportionAmount();

//    alert(SelectedAmount+'----'+ApplyAmount);
    if (Math.abs(SelectedAmount) > 0 && SelectedAmount < ApplyAmount) {
        document.getElementById("cmdSubmit").style.display = "block";
        var rowob = document.getElementsByName("txtMaterielApportion");
        var rowcount = rowob.length;
        var temp = SelectedAmount / ApplyAmount;

        for (var i = 0; i < rowcount; i++) {
            if (rowob[i].name == "txtMaterielApportion") {
                document.getElementById(rowob[i].id).value = Math.round(document.getElementById(rowob[i].id + "MaterielApportion").value * temp * 1000) / 1000;
            }
        }
    }
    
    GetSelectedAmount();
    TradingDeliveryApply();
}

function GetSelectedMaterielApportionAmount() {
    GetSelectedAmount();
    var all = document.getElementById("txtApprovalSelectedAmount").value;

    var rowob = document.getElementsByName("txtMaterielApportion");
    var rowcount = rowob.length;
    var all2 = 0;

    for (var i = 0; i < rowcount; i++) {
        if (rowob[i].name == "txtMaterielApportion") {
            all2 = all2 + document.getElementById(rowob[i].id).value * document.getElementById("TradingUnitPrice" + rowob[i].id.substring(3)).value;
        }
    }
    var temp = Math.round((all - all2) * 100000) / 100000;
            
    return temp;
}


function ShowCreditCheck(btnid, creditcheckstatevalue, customerno, contractno, sono, deliveryno) {
    var w = document.body.scrollWidth;
    var h = document.body.scrollHeight;
    var obj = document.getElementById("creditcheckdiv");
    obj.style.display = "block";
    obj.style.width = w;
    obj.style.height = h;
    var aa = DeliveryApplyWebService;
    aa.ShowCreditCheck(btnid, creditcheckstatevalue, customerno, contractno, sono, deliveryno, ShowCreditCheckResult);
}
function ShowCreditCheckResult(result) {
    if (result == null || result.length == 0 || result[0] == "0") {
        alert("获取数据失败，请重新操作！");
        showOrHidden('creditcheckdiv', 'none');
    }
    else {
        document.getElementById("creditcheckdiv").innerHTML = result[1];
    }
}

function CreditCheckApproval(flag, btnid, contractno, sono, deliveryno, customerno) {
    document.getElementById("txtCreditBtn").value = btnid;
    var usercode = document.getElementById("txtUserCode").value;
    var coCode = document.getElementById("txtCoCode").value;
    var customeramount = document.getElementById("txtcreditcustomer").value;
    var orederamount = document.getElementById("txtcreditorder").value;
    var aa = DeliveryApplyWebService;
    aa.CreditCheckApproval(flag, coCode, usercode, contractno, sono,
        deliveryno, customerno, customeramount, orederamount, CreditCheckApprovalResult);
}
function CreditCheckApprovalResult(result) {
    if (result = null || result.length == 0) {        
        alert("提交失败，请重新操作！");
    }
    else {
        showOrHidden('creditcheckdiv', 'none');
        document.getElementById(document.getElementById("txtCreditBtn").value).value = "Checked";
    }
}

function CheckOrderCredit(orgamount) {
    var ordercreditamount = document.getElementById("txtcreditorder").value;
    if (ordercreditamount > orgamount) {
        document.getElementById("txtcreditorder").value = orgamount;
        document.getElementById("txtcreditcustomer").value = ordercreditamount - orgamount;
    }
}